//跳转URL
var jumpurl_handle;
function jumpurl (url, time) {
	var timeout = time > 0 ? time * 1000 : 0;
	loading();
	jumpurl_handle = setTimeout(function(){
		clearTimeout(jumpurl_handle);
		if(url){
			location.href = url;
		}else{
			history.go(-1);
		}
	}, timeout);
}
(function (doc, win) {
	var docEl = doc.documentElement,
	resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
	recalc = function () {
		var clientWidth = docEl.clientWidth;
			clientWidth = clientWidth > 640 ? 640 : clientWidth;
		if (!clientWidth) return;
			docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
		};
	if (!doc.addEventListener) return;
	win.addEventListener(resizeEvt, recalc, false);
	doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

$(document).ready(function(){
	//提示信息JS
	$.getScript("/Public/Wap/js/msgbox/msgbox.js",function () {
		$('.jump').live('click',function () {
			var url = $(this).attr('href');
				url = url ? url : $(this).attr('url');
			jumpurl(url);
			return false;
		});
		//链接点击事件
		$('a').live('click', function () {
			var url 	= $(this).attr('href');
				url 	= url ? url : '';
			var is_jump = true;
			var target  = $(this).attr('target');
				target  = target ? target : '';
			if($(this).hasClass('nojump')){is_jump = false;}
			if($(this).hasClass('opt-tip')){is_jump = false;}
			if($(this).parent().hasClass('ajax-page')){is_jump = false;}
			if(target){is_jump = false;}
			if(url == '#'){is_jump = false;}

			if(is_jump && url){
				jumpurl(url);
				return false;
			}
		});
	});
	
	//加载更多
	$(".load-more").click(function () {
		loading();
		var here    = $(this);
		var url     = here.attr('url');
		var data_to = here.attr('data-to');
		if(!url){
			msgbox('URL未配置！');
		}
		$.ajax({
			type: "get",
			url: url,
			dataType:"json",
			success: function(data){
				removeLoading();
				if(data.status==200){
					$("."+data_to).append(data.data.html);
					next_url = data.data.page.ajax_next_page;
					if(next_url){
						here.attr('url',next_url);
					}else{
						msgbox('到底了');
						here.remove();
					}
				}
			},
			error : function () {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
			}
		});
	});

	//ajax表单提交
	$('form.ajax-form').live('submit',function () {
		var url    = $(this).attr('action');
		var method = $(this).attr('method');
		var method = method ? method : 'post';
		var data   = $(this).serialize();

		loading('正在提交，请稍候...');
		$.ajax({
			url : url,
			data : data,
			type : method == 'post' ? 'post' : 'get',
			dataType:'json',
			success : function(json){
				removeLoading();
				if(json.status == 1){
					msgbox(json.info);
					//有返回链接，则跳转
					if(json.url){
						jumpurl(json.url, 2);
					}
				}else{
					msgbox(json.info, 'error');
				}
			},
			error : function () {
				removeLoading();
				msgbox('请求失败，请稍候再试！', 'error');
			}
		});
		return false;
	});

	//操作提示
	$(".opt-tip").click(function(){
		var url = $(this).attr('tip-url');
		var loading_tip = $(this).attr('tip-loading');
		var tourl = $(this).attr('jumpurl');
		url = url ? url : $(this).attr('href');
		loading_tip = loading_tip ? loading_tip : '正在操作，请稍候...';
		if (!url) {
			return false;
		}
		loading(loading_tip);
		$.ajax({
			type: "get",
			url: url,
			dataType: "json",
			success : function(data){
				removeLoading();
				if(data.status == 1){
					msgbox(data.info);
					if(tourl){
						jumpurl(tourl);
					}
				}else{
					msgbox(data.info,'error');
				}
			},
			error :function (data) {
				removeLoading();
				msgbox('请求失败，请稍候再试！','error');
			}
		});
		return false;
	});
});